package com.sghd.candy.module.element.facade;

import com.sghd.candy.module.bag.manager.PlayerBag;
import com.sghd.candy.module.bag.service.PlayerBagService;
import com.sghd.candy.module.common.event.PushEvent;
import com.sghd.candy.module.element.manager.PlayerElement;
import com.sghd.candy.module.element.service.PlayerElementService;
import com.sghd.candy.module.player.manager.Player;
import com.sghd.candy.module.player.service.PlayerService;
import com.sghd.common.event.EventBus;
import com.sghd.common.ramcache.aop.AutoLocked;
import com.sghd.pojo.candy.bag.enums.PropType;
import com.sghd.pojo.candy.element.vo.ElementVo;
import com.sghd.pojo.fight.game.ChessType;
import com.sghd.pojo.fight.game.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.sghd.candy.utils.SocketEnum.BAG_INFO;
import static com.sghd.candy.utils.SocketEnum.ELEMENT_LIST;

/**
 * @author Allen
 * @date 2018/12/28
 */
@Component
public class ElementFacadeImpl implements ElementFacade {
    @Autowired
    private PlayerService playerService;
    @Autowired
    private PlayerElementService playerElementService;
    @Autowired
    private PlayerBagService playerBagService;
    @Autowired
    private EventBus eventBus;
    @Override
    public Result<String> upgrade(long playerId, ChessType chessType) {
        Player player = playerService.load(playerId);
        Result<String> result = playerElementService.upgrade(player, chessType);

        if (result.getCode() == 0) {
            PlayerBag playerBag = playerBagService.getPlayerBag(player.getPlayerId());
            Map<Integer, Long> propMap = new HashMap();
            propMap.put(PropType.MONEY.ordinal(), playerBag.getMoney());
            propMap.put(PropType.DIAMOND.ordinal(), playerBag.getDiamond());
            eventBus.post(PushEvent.valueOf(propMap, player.getPlayerId(), BAG_INFO));

            ElementVo elementVo =  elementList(playerId);
            eventBus.post(PushEvent.valueOf(elementVo, player.getPlayerId(), ELEMENT_LIST));
        }

        return result;
    }

    @Override
    public ElementVo elementList(long playerId) {
        PlayerElement playerElement = playerElementService.getPlayerElement(playerId);
        ElementVo elementVo = new ElementVo();
        elementVo.setDianShu(playerElement.getDianShu());
        elementVo.setElements(playerElement.getElements());
        return elementVo;
    }
}
